package com.example.idemfactor.app.controller;

import com.example.idemfactor.app.service.impl.StringIdmtFallbackImpl;
import com.example.idemfactor.app.vo.TestVo;
import com.example.idemfactor.sdk.annotations.Idemfactor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index")
public class IdemController {

    @GetMapping(value = "/test01")
    @Idemfactor(expireTime = 20l)
    public String test01() {
        return "test01";
    }

    // http://127.0.0.1:8080/idempotent/test02?msg=ssssss
    @GetMapping(value = "/test02")
    @Idemfactor(expireTime = 20l,key="'test02:msg='+#msg")
    public String test02(String msg) {
        if (StringUtils.isEmpty(msg)) {
            System.out.println(msg);
        }
        return "test02";
    }

    // http://127.0.0.1:8080/idempotent/test03?name=1&age=2
    @GetMapping(value = "/test03")
    @Idemfactor(key = "'test03:age='+#testVo.age+';name='+#testVo.name",expireTime = 20l)
    public String test03(TestVo testVo) {
        System.out.println(testVo);
        return "test03";
    }

    // http://127.0.0.1:8080/idempotent/test04?text=sdasda
    @RequestMapping(value = "/test04", method = RequestMethod.GET)
    @Idemfactor(fallback = StringIdmtFallbackImpl.class,expireTime = 20l,key="'text='+#text")
    public String test04(String text) {
        return "test04";
    }


    // http://127.0.0.1:8080/idempotent/test05?name=1&age=2
    @GetMapping(value = "/test05")
    @Idemfactor(keyGenerator = "myKeyGenerator",expireTime = 20l)
    public String test05(TestVo testVo) {
        System.out.println(testVo.toString());
        return "test05";
    }

}
